package day10.Ti.StreamDome.FuJia;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//	使用Stream流的map()方法，将每个元素封装为一个Person对象。
//        	将新流中的元素提取为List集合。
//        	遍历、打印集合中所有的Person对象信息。

public class Text {
    public static void main(String[] args) {
        ArrayList<String>list=new ArrayList<>();
        list.add("杨过");
        list.add("小龙女");
        list.add("乔峰");
        list.add("阿朱");
        list.add("令狐冲");
        list.add("任盈盈");
//        method1(list);
//        method2(list);
//        method3(list);
        Stream<Person> ps = list.stream().map(s -> new Person(s));
        List<Person> collect = ps.collect(Collectors.toList());
        for (Person p : collect) {
            System.out.println(p);
        }

    }

    private static void method3(ArrayList<String> list) {
        Stream<String> stream = list.stream();
        List<Person> collect = stream.map(new Function<String, Person>() {
            @Override
            public Person apply(String s) {
                return new Person(s);
            }
        }).collect(Collectors.toList());
        System.out.println(collect);
    }

    private static void method2(ArrayList<String> list) {
        Stream<Person> personStream = list.stream().map(s -> new Person(s));
        List<Person> collect = personStream.collect(Collectors.toList());
        for (Person person : collect) {
            System.out.println(person);
        }
    }

    private static void method1(ArrayList<String> list) {
        Stream<String> stream = list.stream();
        List<Person> collect = stream.map(new Function<String, Person>() {
            @Override
            public Person apply(String s) {
                return new Person(s);
            }
        }).collect(Collectors.toList());
        System.out.println(collect);
    }
}
